#include "changepersonalinfo.h"
#include "ui_changepersonalinfo.h"
#include "logonwidget.h"
#include "user.h"
#include "friendlist.h"
#include <QHostAddress>
#include <QMessageBox>
changepersonalinfo::changepersonalinfo(QWidget *parent,user userSelf) :
    QDialog(parent),
    ui(new Ui::changepersonalinfo)
{
    ui->setupUi(this);
    this->userSelf = userSelf;
    //connect(ui->btn_cancel,&QPushButton::clicked,this, &changepersonalinfo::on_btn_cancel_clicked);
    //connect(ui->btn_change,&QPushButton::clicked,this, &changepersonalinfo::on_btn_change_clicked);
}

changepersonalinfo::~changepersonalinfo()
{
    delete ui;

}
void changepersonalinfo::on_btn_change_clicked()
{
    //获取输入的信息
    QString regUsername = ui->reg_username_lineEdit->text();
    QString regUserqq=ui->reg_userqq_lineEdit->text();//未完成
    QString regUserBirthDay=ui->reg_userbirthday_lineEdit->text();//未完成
    QString regUserEmail=ui->reg_useremail_lineEdit->text();//未完成
    QString regUserPhoto=ui->reg_userphoto_lineEdit->text();//未完成
    QString senderToken = userSelf.getUserPassword();
    user Sender;
    //建立新的修改信息
    QJsonObject new_info_value{
        {"id",userSelf.getUserId().toInt()},
        {"username", regUsername},
        {"photo", regUserPhoto},
        {"qq",regUserqq},
        {"birthday",regUserBirthDay},
        {"password", ""},
        {"email",regUserEmail},
        {"gender",userSelf.getUserGender().toInt()}
    };
    //qDebug()<<"SenderValue"<<new_info_value;



    //QNetworkRequest request(QUrl("http://localhost:19999/slimarveous/set_profile"));
    QNetworkRequest request(QUrl("http://101.43.168.188:19999/slimarveous/set_profile"));//连接后端set_profile文件



    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json2");//获取http协议请求
    QJsonObject json2{//传到后端的json文件
        {"token",senderToken},
        {"id",userSelf.getUserId().toInt()},
        {"new_info",new_info_value},
    };
    QJsonDocument doc(json2);
    QByteArray data = doc.toJson(QJsonDocument::Compact);
    //qDebug()<<data;
    QNetworkReply *reply = manager.post(request, data);
    QMessageBox:: information(this, "更改成功！","请重新登陆！");
    connect(reply, &QNetworkReply::finished, [=](){});
    //ui->messageSendTextEdit->clear();
    this->close();
    LogOnWidget *low=new LogOnWidget(nullptr);//连接到登录页面
    low->show();
}
void changepersonalinfo::on_btn_return_clicked()//返回friendlist页面
{
    this->close();
    friendlist* w1 = new friendlist(nullptr,userSelf);
    w1->show();
}
void changepersonalinfo::on_btn_cancel_clicked()//清空填的内容
{
    ui->reg_username_lineEdit->clear();
    ui->reg_userbirthday_lineEdit->clear();
    ui->reg_useremail_lineEdit->clear();
    ui->reg_userphoto_lineEdit->clear();
    ui->reg_userqq_lineEdit->clear();
}
